<?php

if (isset($_GET["size"])) {
    $size = $_GET["size"];
} else {
    $size=null;
}

if (isset($_GET["id"])) {
    $id = $_GET["id"];
} else {
    $id=null;
}

$select_img_byName_full = "SELECT fullSize,ext FROM img WHERE name LIKE '%s'";
$select_img_byName_medium = "SELECT mediumSize,ext FROM img WHERE name LIKE '%s'";
$select_img_byName_small = "SELECT smallSize,ext FROM img WHERE name LIKE '%s'";

require_once 'lib/connection.php';
require_once 'lib/SettingsAbstract.php';
require_once 'custom/customSettings.php';
require_once 'lib/includeLast/includeLastSettings.php';
connect();


if ($size == null || $size == ""){
    $size = "small";
}

if ($id == null || $id == ""){
    $id = Settings::$defaultImage;
}

if ($size == "small") {
    $query = sprintf($select_img_byName_small, mysql_real_escape_string($id)
    );
    $result = mq($query);
    while ($row = mysql_fetch_array($result)) {        
        setHeader($row["ext"],base64_decode($row["smallSize"]));
    }
}
if ($size == "medium") {
    $query = sprintf($select_img_byName_medium, mysql_real_escape_string($id)
    );
    $result = mq($query);
    while ($row = mysql_fetch_array($result)) {   
        setHeader($row["ext"],  base64_decode($row["mediumSize"]));        
    }
}
if ($size == "full") {
    $query = sprintf($select_img_byName_full, mysql_real_escape_string($id)
    );
    $result = mq($query);
    while ($row = mysql_fetch_array($result)) {        
        setHeader($row["ext"],base64_decode($row["fullSize"]));
    }    
}


close();

function setHeader($ext,$db_img){
    header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 3600));
    switch ($ext) {
        case "JPG":
            header("Content-Type: image/jpeg");
            $db_img = imagecreatefromstring($db_img);
            imagejpeg($db_img);            
            break;
        case "jpg":
            header("Content-Type: image/jpeg");
            $db_img = imagecreatefromstring($db_img);
            imagejpeg($db_img);            
            break;
        case "jpeg":
            header("Content-Type: image/jpeg");
            $db_img = imagecreatefromstring($db_img);
            imagejpeg($db_img);
            break;
        case "gif":
            header("Content-Type: image/gif");
            $db_img = imagecreatefromstring($db_img);
            imagegif($db_img);
            break;
        case "png":
            header("Content-Type: image/png");
            $db_img = imagecreatefromstring($db_img);
            imagepng($db_img);
            break;
        case "PNG":
            header("Content-Type: image/png");
            $db_img = imagecreatefromstring($db_img);
            imagepng($db_img);
            break;
    }
    imagedestroy($db_img);
}
?>
